/**
 * Created by Administrator on 2015/9/1.
 */
var express = require('express');
var router = express.Router();

var cusService = require('../src/service/cusService');
/**活动明细*/
router.get('/active/activityDetail', function(req, res, next) {
    cusService.queryActiveDetail(req,function(result){
        res.render('EJS/customer/active/activityDetail', { active: result });
    })
});
/**活动明细(可报名)*/
router.get('/active/activityDetailAndApply', function(req, res, next) {
    cusService.queryActiveDetail(req,function(result){
        res.render('EJS/customer/active/activityDetailAndApply', { active: result });
    })
});
/**活动列表*/
router.get('/active/activityList', function(req, res, next) {
    cusService.queryActiveList(req,function(result,param){
        res.render('EJS/customer/active/activityList', { actives: result,param:param});
    })
});
/**查看与当前登录用户有关的活动*/
router.get('/active/applyInvited',function(req,res){
    cusService.queryActiveAboutMe(req,function(result,param){
        res.render('EJS/customer/active/myActivityList', { actives: result ,param : param});
    })
});
/**查看与将要开始的活动*/
router.get('/active/comingActivityList',function(req,res){
    cusService.queryComingActive(req,function(result,param){
        console.log(param+"controller");
        res.render('EJS/customer/active/comingActivityList', { actives: result , param : param });
    })
});

/**活动统计*/
router.get('/active/statistic', function (req, res, next) {
    cusService.queryStatistic(req, function (result) {
        res.render('EJS/customer/active/statistic', {actives: result});
    });
});
/**更新密码*/
router.get('/personalSetting/toUpdatePassword', function(req, res, next) {
    res.render('EJS/customer/personalSetting/updatePassword', {r_code:1});
});
/**检查密码*/
router.post('/personalSetting/checkPassword', function (req, res, next) {
    cusService.queryPassword(req, res, function (a) {
        var obj1 = eval(a);
        //console.log(obj1.code);
        res.send(obj1);
    })
});
/**更新密码*/
router.post('/personalSetting/updatePassword', function (req, res, next) {
    cusService.updatePassword(req, res, function (a) {
        var obj1 = eval(a);
        console.log(obj1.code);
        res.render('EJS/customer/personalSetting/updatePassword', {title: '修改用户密码',r_code:obj1.code});
    })
});
/**更新个人信息get方法*/
router.get('/personalSetting/toUpdatePersonInfo', function(req, res, next) {
    res.render('EJS/customer/personalSetting/updatePersonInfo', {title: '个人信息修改',users: 1,user: req.session.currentUser});
});
/**更新个人信息post方法*/
router.post('/personalSetting/updatePersonInfo', function(req, res, next) {
    cusService.updatePersonInfo(req, res, function (a) {
        var obj1=eval(a);
        res.render('EJS/customer/personalSetting/updatePersonInfo', {title: '个人信息修改', users:obj1.code,user:req.session.currentUser});
    })
});
/**检查手机号码是否重复*/
router.post('/personalSetting/checkTel', function (req, res, next) {
    cusService.queryTel(req, res, function (a) {
        var obj1 = eval(a);
        res.send(obj1);
    })
});
/**改变活动邀请/报名的状态*/
router.get('/active/changeAdmitStatus', function(req, res, next) {

    var status, id, activeId;
    if (req.param('param') != undefined) {
        var param = req.param('param').split(";");
        status = param[1];
        id = param[3];
        activeId = param[5];
    } else {
        status = req.param('status');
        id = req.param('id');
    }
    if(req.session.currentUser.level==3 && status==8){
        res.redirect("/toLogin?code=4");
    }else{
        cusService.changeAdmitStatus(req,function(result,status){
            if(status==8){
                res.send("签到成功");
            }
            else{
                res.redirect('/customer/active/applyInvited');
            }
        });
    }

});

/**取消报名*/
router.get('/active/cancelApply', function(req, res, next) {
    cusService.cancelApply(req,function(result){
        res.redirect('/customer/active/applyInvited');
    });
});
/**用户报名参与活动*/
router.get('/active/applyActive', function (req, res, next) {
    console.log('success');
    cusService.applyActive(req,function(result){
        res.redirect('/customer/active/applyInvited');
    });
});


/**查看二维码*/
router.get('/active/queryQrCode', function(req, res, next) {
    //res.render('EJS/customer/personalSetting/updatePersonInfo', { title: 'Express' });

    res.render('EJS/customer/active/qrCode', { admitId: req.param("admitId"),activeId:req.param('activeId'),userId:req.session.currentUser.id });
});
//活动列表搜索
router.post('/active/activityList', function (req, res, next) {
    cusService.queryActiveByName(req , function (result) {
        res.render('EJS/customer/active/activityListByName', {actives: result});
    });
});


module.exports = router;